package com.imonsoft.sqm.service.basedata;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.sqm.dao.basedata.SupplierDAO;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.pojo.basedata.SupplierExample;
import com.imonsoft.sqm.web.excel.SupplierExcel.SupplierImportDto;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private SupplierDAO supplierDAO;

	@Override
	public void stopSupplier(List<Long> idList) {
		if (null == idList || idList.isEmpty()) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		for (int i = 0; i < idList.size(); i++) {
			Supplier supplier = supplierDAO.selectSupplierByPrimaryKey(idList.get(i));
			if (!supplier.getState().equals(ConstantSqm.STATUS_NORMAL)) {
				throw new SystemException(ConstantSqm.OPERATE_STOP);
			}
			supplier.setState(ConstantSqm.STATUS_DELETE);
			supplierDAO.updateSupplierByPrimaryKey(supplier);
		}
	}

	@Override
	public void startSupplier(List<Long> idList) {
		if (null == idList || idList.isEmpty()) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		for (int i = 0; i < idList.size(); i++) {
			Supplier supplier = supplierDAO.selectSupplierByPrimaryKey(idList.get(i));
			if (!supplier.getState().equals(ConstantSqm.STATUS_DELETE)) {
				throw new SystemException(ConstantSqm.OPERATE_START);
			}
			supplier.setState(ConstantSqm.STATUS_NORMAL);
			supplierDAO.updateSupplierByPrimaryKey(supplier);
		}
	}

	@Override
	public List<Supplier> findSupplierByCode(String code) {
		if (StringUtils.isEmpty(code)) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		SupplierExample example = new SupplierExample();
		SupplierExample.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(code)) {
			criteria.andCodeEqualTo(code);
		}
		List<Supplier> supplierList = supplierDAO.selectSupplierByExample(example);
		return supplierList;
	}

	@Override
	public Long insert(Supplier entity) {
		if (null == entity) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		entity.setState(ConstantSqm.STATUS_NORMAL);
		return supplierDAO.insertSupplier(entity);
	}

	@Override
	public int update(Supplier entity) {
		if (null == entity || null == entity.getId()) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		Supplier supplier = supplierDAO.selectSupplierByPrimaryKey(entity.getId());
		supplier.setCode(entity.getCode());
		supplier.setName(entity.getName());
		supplier.setMail(entity.getMail());
		supplier.setPhone(entity.getPhone());
		supplier.setLinkman(entity.getLinkman());
		supplier.setMaterialGroup(entity.getMaterialGroup());
		return supplierDAO.updateSupplierByPrimaryKey(supplier);
	}

	@Override
	public int delete(Long id) {
		if (null == id) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		return supplierDAO.deleteSupplierByPrimaryKey(id);
	}

	@Override
	public Supplier get(Long id) {
		if (null == id) {
			throw new SystemException(ConstantSqm.DATA_ERROR);
		}
		Supplier supplier = supplierDAO.selectSupplierByPrimaryKey(id);
		return supplier;
	}

	@Override
	public List<Supplier> findList(Supplier entity) {
		SupplierExample example = new SupplierExample();
		SupplierExample.Criteria criteria = example.createCriteria();
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
		if (null != entity) {
			if (!StringUtils.isEmpty(entity.getCode())) {
				criteria.andCodeLike(entity.getCode() + "%");
			}
		}
		List<Supplier> supplierList = supplierDAO.selectSupplierByExample(example);
		return supplierList;
	}

	@Override
	public DataGrid<Supplier> getDataGrid(Supplier entity, Pager pager) {
		SupplierExample example = new SupplierExample();
		example.setOrderByClause("code desc");
		SupplierExample.Criteria criteria = example.createCriteria();
		if (null != entity) {
			if (!StringUtils.isEmpty(entity.getCode())) {
				criteria.andCodeLike(entity.getCode() + "%");
			}
			if (!StringUtils.isEmpty(entity.getName())) {
				criteria.andNameLike(entity.getName() + "%");
			}
			if (!StringUtils.isEmpty(entity.getMaterialGroup())) {
				criteria.andMaterialGroupEqualTo(entity.getMaterialGroup());
			}
			if (!StringUtils.isEmpty(entity.getState())) {
				criteria.andStateEqualTo(entity.getState());
			}
		}
		return supplierDAO.getDataGrid(example, pager);
	}

	/**
	 * zhouwei 系统导入保存
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void txInsertForSys(List<SupplierImportDto> importList) {
		if (null == importList) {
			throw new SystemException("提交数据不能为空");
		}
		if (importList != null && importList.size() > 0) {
			for (SupplierImportDto s : importList) {
				Supplier supplier = new Supplier();
				supplier.setCode(s.getCode());
				List<Supplier> checkHave = findList(supplier);
				if (checkHave != null && checkHave.size() > 0) {
					continue;
				}
				BeanUtils.copyProperties(s, supplier);
				supplier.setState(ConstantSqm.STATUS_NORMAL);
				supplierDAO.insertSupplier(supplier);
			}
		}
	}

	/**
	 * 查询所有
	 */
	@Override
	public List<Supplier> queryAll() {
		SupplierExample example = new SupplierExample();
		List<Supplier> supplierList = supplierDAO.selectSupplierByExample(example);
		return supplierList;
	}

	@Override
	public List<Supplier> getAllSupplier( Pager pager) {
		SupplierExample example = new SupplierExample();
		example.setOrderByClause("id desc");
		SupplierExample.Criteria criteria = example.createCriteria();
		criteria.andStateEqualTo(ConstantSqm.STATUS_NORMAL);
		
		return supplierDAO.getAllSupplier(example, pager);
	}
}
